﻿//-------------------------------------------------------
// Write data table in XML format
//-------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Globalization;

namespace RowCountDataCollector
{
    class RowCountXMLWriter
    {
        #region Public methods

        static DataTable m_dataTable;

        // Create a data table
        public static void CreateDataTable()
        {
            m_dataTable = new DataTable();

            m_dataTable.Locale = CultureInfo.InvariantCulture;
            m_dataTable.Columns.Add(new DataColumn("Description", typeof(string)));
            m_dataTable.Columns.Add(new DataColumn("RowCount", typeof(Int32)));
        }

        // Add a row to a data table
        public static void AddDataRow(string description, int rowCount)
        {
            DataRow row = m_dataTable.NewRow();
            row["Description"] = description;
            row["RowCount"] = rowCount;
            m_dataTable.Rows.Add(row);
        }

        // Write tabular data to XML format
        public static void WriteRowCountsToXmlFile(string xmlFilePath)
        {
            DataSet dataSet = new DataSet();
            dataSet.Locale = CultureInfo.InvariantCulture;
            dataSet.Tables.Add(m_dataTable);

            using (System.IO.StreamWriter xmlStreamWriter = new System.IO.StreamWriter(xmlFilePath))
            {
                dataSet.WriteXml(xmlStreamWriter, XmlWriteMode.WriteSchema);
                xmlStreamWriter.Close();
            }

            dataSet.Dispose();
        }
        #endregion
    }
}



